package com.powernode.common;

import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

/**
 * @Description TODO
 * @Date 2022/4/8 23:22
 * @Version 1.0
 */
@Component
public class EmailListener {

    @Value("${spring.mail.username}")
    String from;

    @Resource
    JavaMailSender javaMailSender;

    @RabbitListener(queues = "email")
    public void handler(Message message) throws UnsupportedEncodingException {
        String content = new String(message.getBody(), "utf8");

        LxlEmail lxlEmail = JSONObject.parseObject(content, LxlEmail.class);

        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setTo(lxlEmail.getTo());
        mailMessage.setSubject(lxlEmail.getSubject());
        mailMessage.setText(lxlEmail.getText());
        mailMessage.setFrom(from);

        javaMailSender.send(mailMessage);

    }
}
